package com.gpt.annotation.aspect;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {
   // 定义切入点(描述连接点的集合)，匹配HelloService中所有方法
   @Pointcut("execution(* com.gpt.annotation.service.HelloService.*(..))")
   public void helloServiceMethods(){}

   //前置通知
   @Before("helloServiceMethods()")
   public void beforeAdvice(){
      System.out.println("Before method execution...");
   }

   //后置通知
   @After("helloServiceMethods()")
   public void afterAdvice(){
      System.out.println("After method execution...");
   }

   //返回通知
   @AfterReturning("helloServiceMethods()")
   public void afterReturningAdvice(){
      System.out.println("After method returns normally...");
   }

   //异常通知
   @AfterThrowing("helloServiceMethods()")
   public void afterThrowingAdvice(){
      System.out.println("After method throws exception...");
   }

   //环绕通知
   @Around("helloServiceMethods()")
   public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
      System.out.println("Around before...");
      Object result = pjp.proceed();
      System.out.println("Around after...");
      return result;
   }
}
